package com.blueocean_health.care.management.controller;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.blueocean_health.care.management.common.base.em.TimeType;
import com.blueocean_health.care.management.common.base.time.TimeUtils;
import com.blueocean_health.care.management.common.excel.ExcelUtil;
import com.blueocean_health.care.management.domain.base.ReserveFunds;
import com.blueocean_health.care.management.domain.base.SysResult;
import com.blueocean_health.care.management.domain.base.SysResultByPage;
import com.blueocean_health.care.management.domain.dto.ReserveFundsDto;
import com.blueocean_health.care.management.domain.dto.ReserveFundsDvo;
import com.blueocean_health.care.management.service.ReserveFundsService;
import com.github.pagehelper.PageInfo;

@RestController
@RequestMapping("web/funds")
public class ReserveFundsController {
	
	@Resource
	private ReserveFundsService reserveService;
	
	/**
	 * 根据条件进行查询  2017年12月6日18:42:06
	 * @param map
	 * @return
	 */
	@RequestMapping("list")
	public SysResult getAllCashRecord(@RequestBody ReserveFundsDvo fundsDvo){
		
		PageInfo<ReserveFundsDto> allReserveFunds = reserveService.getAllReserveFunds(fundsDvo);
		
		return SysResultByPage.ok(0, "success", allReserveFunds.getList(), allReserveFunds.getTotal());
	}

	/**
	 * 新增备用金信息   2017年12月6日16:24:11
	 * @param reserveFunds
	 * @return
	 */
	@RequestMapping("add")
	public SysResult addReserveFunds(@RequestBody ReserveFunds reserveFunds){
		int funds = reserveService.insertReserveFunds(reserveFunds);
		if(funds <= 0){
			return SysResult.error(1, "保存备用金信息失败");
		}else{
			return SysResult.ok(0, "success");
			
		}
	}
	/**
	 * 根据Id修改备用金信息   2017年12月6日17:37:50
	 * @param reserveFunds
	 * @return
	 */
	@RequestMapping("update")
	public SysResult updateReserveFunds(@RequestBody ReserveFunds reserveFunds){
		int funds = reserveService.updateReserveFundsById(reserveFunds);
		if(funds <= 0){
			return SysResult.error(1, "修改备用金信息失败");
		}else{
			return SysResult.ok(0, "success");
		}
	}
	/**
	 * 根据Id修改备用金信息   2017年12月6日17:37:50
	 * @param reserveFunds
	 * @return
	 */
	@RequestMapping("getFundsById")
	public SysResult getReserveFundsById(@RequestBody Map<String,String> map){
		String id = map.get("id");
		
		ReserveFunds reserveFunds = reserveService.getReserveFundsById(Integer.parseInt(id));
		return SysResult.ok(0, "success",reserveFunds);
	}
	
	/**
	 * 根据Id删除备用金信息  2017年12月6日18:13:31
	 * @param reserveFunds
	 * @return
	 */
	@RequestMapping("delete")
	public SysResult deleteReserveFundsById(@RequestBody Map<String,String> map){
		String id = map.get("id");
		int delete = reserveService.deleteReserveFundsById(Integer.parseInt(id));
		if(delete <= 0){
			return SysResult.error(1, "删除信息失败");
		}else{
			return SysResult.ok(0, "success");
		}
	}
	/**
	 * 导出备用金 Excel  2017年12月7日10:07:32
	 * @param map
	 * @return
	 */
	@RequestMapping("fundsExcel")
	public void getReserveFundsExcel(HttpServletRequest request,HttpServletResponse response){
		try{
			Map<String,String> map = new HashMap<>();
			map.put("startTime", request.getParameter("startTime"));
			map.put("endTime", request.getParameter("endTime"));
			map.put("reserveFeeType", request.getParameter("reserveFeeType"));
			map.put("feeMin", request.getParameter("feeMin"));
			map.put("feeMax", request.getParameter("feeMax"));
			map.put("manager", request.getParameter("manager"));
			map.put("haveBill", request.getParameter("haveBill"));
			List<ReserveFundsDto> reserveFundsExcel = reserveService.getReserveFundsExcel(map);
			String finalFileName = URLEncoder.encode("备用金列表_"+TimeUtils.getTime(TimeType.yyyyMMddHHmmssSSS)+".xls", "UTF8");
			response.addHeader("Content-Disposition", "attachment;filename=" + finalFileName);
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			
			ExcelUtil.getInstance().exportObj2Excel(toClient, reserveFundsExcel, ReserveFundsDto.class);
		}catch(IOException e){
			e.printStackTrace();
		}
		
	}
}
